/*
 * Cricket.h
 * Michel Goraczko <michelg@csail.mit.edu>
 *
 * Cricket modules 
 *   
 */

/*
      This software may be used and distributed according to the terms
      of the GNU General Public License (GPL), incorporated herein by reference.
      Drivers based on this skeleton fall under the GPL and must retain
      the authorship (implicit copyright) notice.

      This program is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      General Public License for more details.
*/



#ifndef _CRICKET_H
#define _CRICKET_H

/* Basic constants to control the car interface */
#define MOVING_TIME         3000    
#define MOVE_TIME           3000    

#include <stdio.h>
#include <avr/eeprom.h>

#define SYN1 0x0f
#define SYN2 0x0e

#define CRICKET_SOFTWARE_VERSION "2.2.0"

// The speed of sound used for distance calculation
#define SPEED_OF_SOUND 342
// The offset due to overhead
//#define TIMER_OFFSET 5218
#define TIMER_OFFSET 550
// Compensation that represent the time a bit takes to travel over the radio
#define COMPENSATION_VAL 48
// Multiplier as a given speed of sound (currently 342)
#define DISTANCE_MULT 30
// The maximum amount of that to wait for ultrasound arrival
// The should be the time to travel the maximum distance + the offset
// Currently this is 45000 us + the offset
#define MAX_ALLOWED_TIMER_VAL ((unsigned int)30000 + (unsigned int)30000 + (unsigned int) TIMER_OFFSET)
// The average beacons time (in ms)
#define AVG_BEACON_INTERVAL_TIME 1000
// This is the delta (((MAX_ALLOW_TIMER_VAL -
// TIMER_OFFSET) / 1000) * MAX_NUMBER_OF_BEACON % 2)
#define DELTA_BEACON_INTERVAL_TIME 664
// Minimum beacon interval time
// This is eval to AVG_BEACON_INTERVAL_TIME - DELTA_BEACON_INTERVAL_TIME / 2
#define MIN_BEACON_INTERVAL_TIME 668
// This is eval to AVG_BEACON_INTERVAL_TIME + DELTA_BEACON_INTERVAL_TIME / 2
#define MAX_BEACON_INTERVAL_TIME 1332
// Maximum ultrasound travel time
#define MAX_US_TRAVEL_TIME ((unsigned int)23000 + (unsigned int)20000 + (unsigned int)10000)
// The maximum number of beacon that can be present in the neighborhood
#define MAX_NUMBER_OF_BEACONS 22
// Desync delay in microseconds.
#define DESYNC_DELAY 500


// UNITS
#define DIST_METRIC 0x01
#define DIST_IMPERIAL 0x02
#define TIME_US 0x11
#define TIME_MS 0x12
#define TIME_SS 0x13
#define TEMP_F  0x21
#define TEMP_C  0x22

// Beacon MODE
#define MODE_BEACON 1
#define MODE_LISTENER 2

#define MINUTES (600 * 100)
#define HOURS (3600 * 1000)

struct CricketBeaconResponse {
      uint8_t Space[8];
      uint16_t Distance;
      uint16_t MsgId;
      uint16_t TrackCount;
      uint16_t Other;
};

struct CricketBeacon {
	      uint8_t Space[8]; // 8
	      uint8_t Id[8];   	// 8
      	      uint16_t x;     	// 2 
      	      uint16_t y;    	// 2
              uint16_t z;    	// 2
              int8_t temp;	// 1 
	      bool tb;
};


typedef struct CricketConfig_s {
	char version[4];
	uint8_t spaceid[8];
	uint8_t run_mode;
	uint16_t speed_of_sound;
	uint16_t timer_offset;
	uint16_t compensation_val;
	uint16_t distance_mult;
	uint16_t max_allowed_timer_val;
	uint16_t avg_beacon_interval_time;
	uint16_t delta_beacon_interval_time;
	uint16_t min_beacon_interval_time;
	uint16_t max_beacon_interval_time;
	uint16_t max_us_travel_time;
	uint8_t max_beacon_number;
	uint8_t time_units;
	uint8_t distance_units;
	uint8_t temp_units;
	uint8_t outputformat;
	uint16_t x;
	uint16_t y;
	uint16_t z;
	float local_temp;
	bool use_temp_sensor;
	bool output_vr;
	bool output_db;
	bool output_dr;
	bool output_sp;
	bool output_id;
	bool output_tm;
	bool output_tp;
	bool output_ts;
	bool output_pc;
	bool output_tb;
	uint8_t ultrasound_gain;
} CricketConfig_s;


  
uint8_t debug_out = 1;

#define UARTOutput(__x,__y,__args...) do { \
    static char __s[] PROGMEM = __y; \
    if (debug_out) \
        printf_P(__s, ## __args);       \
        } while (0)

#endif
